package com.himalayan.bear.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.himalayan.bear.common.util.StringUtils;
import com.himalayan.bear.dao.FolderMapper;
import com.himalayan.bear.entity.Folder;
import com.himalayan.bear.service.FolderService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zjk
 * @since 2019-05-30
 */
@Service
public class FolderServiceImpl extends ServiceImpl<FolderMapper, Folder> implements FolderService {

    @Override
    public List<Map<String, Object>> parentList(String folderId) {
        return baseMapper.parentList(folderId);
    }

    @Override
    public boolean isExistsByCode(String code, String parentId) {
        QueryWrapper<Folder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code)
                .eq("parent_id", parentId)
                .eq("delete_flag", "0");
        return getOne(queryWrapper) != null;
    }

    @Override
    public String loadNewName(String folderName) {
        if (StringUtils.isEmpty(folderName)) {
            return null;
        }
        String reg = "^.*（\\d+）$";
        boolean r = folderName.matches(reg);
        if (r) {
            String num = StringUtils.substringBetween(folderName, "（", "）");
            int count = Integer.parseInt(num);
            count += 1;
            return folderName.replaceAll("（\\d+）", "（" + count + "）");
        }
        return folderName + "（2）";
    }
}
